package org.fast.project.web.action;

import jakarta.validation.Valid;

import org.fast.project.service.UserInfoService;
import org.fast.project.service.bo.AddUserDO;
import org.fast.project.service.bo.FindUserDO;
import org.fast.project.service.bo.UpdateUserDO;
import org.fast.project.web.transfer.UserDo2VoTransfer;
import org.fast.project.web.vo.CommonResponse;
import org.fast.project.web.vo.PageVO;
import org.fast.project.web.vo.UserVO;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : lilith·bristol
 * @program : spring-web-archetype-action
 * @description : 用户数据处理
 * @date : 2020-04-20 01:04
 **/

@RestController
@RequestMapping("user")
public class UserAction {

    private final UserInfoService userInfoService;

    private final UserDo2VoTransfer userDo2VoTransfer;

    public UserAction(UserInfoService userInfoService, UserDo2VoTransfer userDo2VoTransfer) {
        this.userInfoService = userInfoService;
        this.userDo2VoTransfer = userDo2VoTransfer;
    }

    /**
     * 获取用户
     *
     * @param id id
     * @return {@link CommonResponse}<{@link UserVO}>
     */
    @GetMapping("/{id}")
    public CommonResponse<UserVO> getUser(@PathVariable("id") Integer id) {
        var res = userInfoService.getUser(id);
        return CommonResponse.createSuccessResponse(userDo2VoTransfer.doToVo(res));
    }

    /**
     * 添加用户
     *
     * @param addUserDO 添加用户模型对象
     * @return {@link CommonResponse}<{@link UserVO}>
     */
    @PostMapping
    public CommonResponse<UserVO> addUser(@RequestBody @Valid AddUserDO addUserDO) {
        var res = userInfoService.addUser(addUserDO);
        return CommonResponse.createSuccessResponse(userDo2VoTransfer.doToVo(res));
    }

    /**
     * 更新用户
     *
     * @param updateUserDO 更新用户模型对象
     * @return {@link CommonResponse}<{@link UserVO}>
     */
    @PutMapping
    public CommonResponse<UserVO> updateUser(@RequestBody @Valid UpdateUserDO updateUserDO) {
        var res = userInfoService.updateUser(updateUserDO);
        return CommonResponse.createSuccessResponse(userDo2VoTransfer.doToVo(res));
    }

    /**
     * 删除用户
     *
     * @param id id
     * @return {@link CommonResponse}<{@link Void}>
     */
    @DeleteMapping("/{id}")
    public CommonResponse<Void> deleteUser(@PathVariable("id") Integer id) {
        userInfoService.deleteUser(id);
        return CommonResponse.createSuccessResponse(null);
    }

    /**
     * 获取用户
     *
     * @param findUserDO 找到用户模型对象
     * @return {@link CommonResponse}<{@link PageVO}<{@link List}<{@link UserVO}>>>
     */
    @GetMapping("/find")
    public CommonResponse<PageVO<List<UserVO>>> getUser(@RequestBody FindUserDO findUserDO) {
        var pageDo = userInfoService.findUser(findUserDO);
        var userVOList = userDo2VoTransfer.doToVo(pageDo.getData());
        return CommonResponse.createSuccessResponse(PageVO.init(pageDo, userVOList));
    }
}
